﻿using System;
using System.Configuration;
using System.Data;
using System.Web.UI;
using KPIS.DBM;
using KPIS.GERP.GIMS.BLL;
using KPIS.GERP.GIMS.MODEL;
using KPIS.GERP.ESS.BLL;

namespace KPIS.GERP.GIMS.APP.MasterPage
{
    public partial class MasterPage : System.Web.UI.MasterPage
    {
        static string strServerName;
        private UserBLL userBLL = new UserBLL();
        private B_MessageSend send = new B_MessageSend();
        private SysMenuItemListBLL bll = new SysMenuItemListBLL();

        protected void Page_Load(object sender, EventArgs e)
        {
            //CheckSession();
            strServerName = Request.Url.Host + (ConfigurationSettings.AppSettings["SubSystem"].ToString().Length > 0 ? "/" + ConfigurationSettings.AppSettings["SubSystem"].ToString() : "");
            cmdInbox.Attributes.Add("onClick", "return false;");
            lbNotification.Attributes.Add("onClick", "return false;");

            string strControlName = ContentPlaceHolder1.Page.ToString();

            if (!IsPostBack)
            {
                imgHeader.Src = "~/App_UI/Images/PageViews/" + ConfigurationSettings.AppSettings["Header"].ToString();

                if (Session["UserSeq"] != null)
                {
                    CreatedMenu(Session["UserSeq"].ToString());
                }
            }

            if (Session["UserName"] != null)
            {
                string strUserName = Session["UserName"].ToString();

                lblUserName.Text = strUserName;
                UpdateUnreadCount();
                cmdInbox.OnClientClick = "openTaskLists('@ServerName', '@Seq')"
                    .Replace("@ServerName", strServerName).Replace("@Seq", Session["EmpSeq"].ToString());
            }
        }

        public void CreatedMenu(string strUserSeq)
        {
            int u = Convert.ToInt32(strUserSeq);
            DataSet ListDataCommand = bll.LoadMenuByUsername(Convert.ToInt32(strUserSeq), "GIMS");
            DataTable table = ListDataCommand.Tables[0];
            RadMenu1.DataSource = ListDataCommand.Tables[0];
            RadMenu1.DataBind();
        }

        protected void UpdateUnreadCount()
        {
            int nUnreadCount = send.GetUnreadCount(Session["EmpSeq"].ToString());
            int nOldUnreadCount = Convert.ToInt32(Session["UnreadCount"]);

            if (nUnreadCount > nOldUnreadCount)
            {
                ShowNotification(nUnreadCount);
            }

            Session["UnreadCount"] = nUnreadCount;
            lblInboxCount.Text = nUnreadCount.ToString();
        }

        protected void ShowNotification(int nUnreadCount)
        {
            lbNotification.Text = "คุณมีข้อความที่ยังไม่ได้อ่าน " + nUnreadCount.ToString();
            RadToolTip1.Show();
        }

        //protected void cmdInbox_Click(object sender, EventArgs e)
        //{
        //    Response.Redirect(@"~\App_UI\Forms\TaskLists.aspx");
        //}

        protected void cmdBackToSelectSystem_Click(object sender, EventArgs e)
        {
            Response.Redirect("//" + strServerName + "/ESS/APP_UI/Forms/SelectSystem.aspx");
        }

        protected string GetSystemName()
        {
            IDBManager dbManager = new DBManager(SystemInfo._DataProvider, SystemInfo._ConnectionString);

            try
            {
                string strResult = "";
                string sql = "select system_name"
                    + " from sys_system_controls";
                DataTable dt = new DataTable();
                dbManager.Open();
                dt = dbManager.ExecuteDataSet(CommandType.Text, sql).Tables[0];

                if (dt.Rows.Count > 0)
                {
                    strResult = dt.Rows[0][0].ToString();
                }

                return strResult;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                dbManager.Dispose();
            }
        }

        private void CheckSession()
        {
            if (Session["EmpSeq"] == null)
            {
                Response.Redirect("//" + strServerName + "/ESS/APP_UI/Forms/Login.aspx");
            }
        }

        //protected void Page_Load(object sender, EventArgs e)
        //{
        //    cmdInbox.OnClientClick = "openTaskLists('@ServerName', '@Seq')"
        //        .Replace("@ServerName", strEssURL).Replace("@Seq", Session["EmpSeq"].ToString());
        //    lbNotification.OnClientClick = "openTaskLists('@ServerName', '@Seq')"
        //        .Replace("@ServerName", strEssURL).Replace("@Seq", Session["EmpSeq"].ToString());
        //    cmdInbox.Attributes.Add("onClick", "return false;");
        //    lbNotification.Attributes.Add("onClick", "return false;");

        //    string strControlName = ContentPlaceHolder1.Page.ToString();

        //    if (strControlName == "ASP.app_ui_forms_login_aspx")
        //    {
        //        RadMenu1.Visible = false;
        //        PanelDetail.Visible = false;
        //    }
        //    else if (strControlName == "ASP.app_ui_forms_selectsystem_aspx")
        //    {
        //        RadMenu1.Visible = false;
        //        PanelDetail.Visible = true;
        //    }
        //    else
        //    {
        //        RadMenu1.Visible = true;
        //        PanelDetail.Visible = true;
        //    }
        //    if (!IsPostBack)
        //    {
        //        HttpCookie cookie = Request.Cookies["UserDetail"];

        //        switch (GetSystemName())
        //        {
        //            case "DEP":
        //            default:
        //                PanelHeader.BackImageUrl = "~/App_UI/Images/PageViews/Header.jpg";
        //                break;
        //            case "ITPF":
        //                PanelHeader.BackImageUrl = "~/App_UI/Images/PageViews/Header_ITPF.jpg";
        //                break;
        //        }
        //        if (cookie != null)
        //        {
        //            Session["EmpSeq"] = cookie["EmpSeq"];
        //            Session["UserName"] = cookie["UserName"];
        //            Session["UserSeq"] = cookie["UserSeq"];
        //            lblUserName.Text = Session["UserName"].ToString();
        //        }
        //        else
        //        {
        //            string name = Session["UserCode"].ToString();                     
        //            string password = Session["UserPass"].ToString();
        //            string UserStatus = "";
        //            DataRowCollection drc;

        //            drc = userBLL.Login(name, password).Tables[0].Rows;

        //            if (drc.Count > 0)
        //            {
        //                UserStatus = drc[0]["user_status"].ToString();

        //                if (UserStatus == SystemInfo._RejectStatus)
        //                {
        //                    //RadManager1.Alert("รอการตรวจสอบจากผู้ดูแลระบบ");
        //                    return;
        //                }
        //                else if (UserStatus == SystemInfo._WorkRecordStatus)
        //                {
        //                    Session["EmpSeq"] = drc[0]["emp_seq"].ToString();
        //                    Session["emp_seq"] = drc[0]["emp_seq"].ToString();
        //                    Session["UserSeq"] = drc[0]["user_seq"].ToString();
        //                    Session["UserCode"] = drc[0]["user_code"].ToString();
        //                    //Page.Response.Redirect("Default.aspx");
        //                }
        //                lblUserName.Text = drc[0]["USER_NAME"].ToString();
        //            }
        //        }
        //        CreatedMenu(Session["UserSeq"].ToString());
        //    }

        //    UpdateUnreadCount();
        //}
        //protected string GetSystemName()
        //{
        //    IDBManager dbManager = new DBManager(SystemInfo._DataProvider, SystemInfo._ConnectionString);

        //    try
        //    {
        //        string strResult = "";
        //        string sql = "select system_name"
        //            + " from sys_system_controls";
        //        DataTable dt = new DataTable();
        //        dbManager.Open();
        //        dt = dbManager.ExecuteDataSet(CommandType.Text, sql).Tables[0];

        //        if (dt.Rows.Count > 0)
        //        {
        //            strResult = dt.Rows[0][0].ToString();
        //        }

        //        return strResult;
        //    }
        //    catch (Exception ex)
        //    {
        //        throw ex;
        //    }
        //    finally
        //    {
        //        dbManager.Dispose();
        //    }
        //}
        //public void CreatedMenu(string strUserSeq)
        //{
        //    int u = Convert.ToInt32(strUserSeq);
        //    DataSet ListDataCommand = bll.LoadMenuByUsername(Convert.ToInt32(strUserSeq),"GIMS");
        //    DataTable table = ListDataCommand.Tables[0];
        //    RadMenu1.DataSource = ListDataCommand.Tables[0];
        //    RadMenu1.DataBind();
        //}

        //protected void UpdateUnreadCount()
        //{
        //    int nUnreadCount = send.GetUnreadCount(Session["EmpSeq"].ToString());
        //    int nOldUnreadCount = Convert.ToInt32(Session["UnreadCount"]);

        //    if (nUnreadCount > nOldUnreadCount)
        //    {
        //        ShowNotification(nUnreadCount);
        //    }

        //    Session["UnreadCount"] = nUnreadCount;
        //    lblInboxCount.Text = nUnreadCount.ToString();
        //}
        //protected void ShowNotification(int nUnreadCount)
        //{
        //    lbNotification.Text = "คุณมีข้อความที่ยังไม่ได้อ่าน " + nUnreadCount.ToString();
        //    RadToolTip1.Show();
        //}
        //protected void cmdLogout_Click(object sender, EventArgs e)
        //{
        //    HttpCookie cookie = Request.Cookies["UserDetail"];

        //    if (cookie != null)
        //    {
        //        cookie.Expires = DateTime.Now.AddDays(-1);
        //    }

        //    RadMenu1.Items.Clear();
        //    Session.Abandon();
        //    Response.Redirect("//" + strEssURL + "/ESS/APP_UI/Forms/Login.aspx");
        //}
        //protected void cmdSelectSystem_Click(object sender, EventArgs e)
        //{
        //    Response.Redirect("//" + strEssURL + "/ESS/APP_UI/Forms/SelectSystem.aspx");
        //}

    }
}
